package com.ruoyi.utils;

import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class WaterMarkUtil {

    private static BufferedImage createWaterMark(String[] content) {
        Integer width = 420;//水印宽度
        Integer height = 380;//水印高度
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取bufferedImage对象
        String fontType = "STSongti-SC-Bold";
        Integer fontStyle = Font.BOLD;
        Integer fontSize = 35;
        Font font = new Font(fontType, fontStyle, fontSize);
        Graphics2D g2d = image.createGraphics(); // 获取Graphics2d对象
        image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = image.createGraphics();
        g2d.setColor(new java.awt.Color(0, 0, 0, 50));
        g2d.setStroke(new BasicStroke(1));
        g2d.setFont(font);
        g2d.rotate(Math.toRadians(-30),(double) image.getWidth() / 2, (double) image.getHeight() / 2);//设置旋转角度

        FontRenderContext context = g2d.getFontRenderContext();
        //找到水印信息中最长的
        int contentLindex = 0;
        int contentLength = content[0].length();

        for (int i = 0; i < content.length; i++) {
            if (content[i].length()>contentLength) {
                contentLindex = i;
            }
        }
        Rectangle2D bounds = font.getStringBounds(content[contentLindex], context);
        double x = (width - bounds.getWidth()) / 2;
        double y = (height - bounds.getHeight()) / 2;
        double ascent = -bounds.getY();
        double baseY = y + ascent;
        // 写入水印文字原定高度过小，所以累计写水印，增加高度
        for (int i = 0; i < content.length; i++) {
            g2d.drawString(content[i], (int)x, (int)baseY);// 画出字符串
            baseY = baseY + font.getSize();
        }

        // 设置透明度
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1F));
        g2d.dispose();
        return image;
    }

    public static void putWaterMarkToExcel(XSSFWorkbook workbook) throws IOException {
        BufferedImage image = createWaterMark(new String[]{"水印名称logo"});
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "png", os);
        int pictureIdx = workbook.addPicture(os.toByteArray(), Workbook.PICTURE_TYPE_PNG);
        POIXMLDocumentPart poixmlDocumentPart = workbook.getAllPictures().get(pictureIdx);
        XSSFSheet sheet = workbook.getSheetAt(0);
        PackagePartName ppn = poixmlDocumentPart.getPackagePart().getPartName();
        String relType = XSSFRelation.IMAGES.getRelation();
        PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);
        sheet.getCTWorksheet().addNewPicture().setId(pr.getId());
    }

}
